package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Schedule;
import cn.kgc.vue.service.ScheduleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 课工场
 * @date 2023/12/15
 * @description
 */
@Api(tags = "开班计划")
@RestController
@RequestMapping("schedules")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @ApiOperation("开班计划列表")
    @PostMapping("list")
    public ResponseResult getScheduleList(@RequestBody QueryCondition<Schedule> queryCondition) {
        return scheduleService.getScheduleList(queryCondition);
    }


    @ApiOperation("添加/修改计划")
    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdateSchedule(@RequestBody Schedule schedule) {

        Integer id = schedule.getId();
        if (ObjectUtils.isNull(id)){
            LambdaQueryWrapper<Schedule> lambda = new QueryWrapper<Schedule>().lambda();
            lambda.eq(Schedule::getClassNo,schedule.getClassNo());
            Schedule one = scheduleService.getOne(lambda);
            if (ObjectUtils.isNotNull(one)){
                return ResponseResult.fail().message("班级编号已存在");
            }
        }
        scheduleService.saveOrUpdate(schedule);
        return ResponseResult.success();
    }


    @ApiOperation("根据id获取开班计划")
    @GetMapping("getScheduleById/{id}")
    public ResponseResult getScheduleById(@PathVariable Integer id) {

        Schedule  schedule = scheduleService.getById(id);
        return ResponseResult.success().data(schedule);
    }


    @ApiOperation("根据id删除开班计划")
    @DeleteMapping("{id}")
    public ResponseResult removeScheduleById(@PathVariable Integer id) {
        scheduleService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }

}
